const config = require('../../../../config')
module.exports = class extends think.Controller {
  async indexAction () { // 检查版本
    var db = this.mongoose('mongoose/version')
    try {
      var arrays = []
      var result = await db.find({type: parseInt(this.get().type)})
      result.forEach(function (item, index) {
        let date1 = new Date(item.time)
        let year = date1.getFullYear()
        let month = date1.getMonth()+1
        month = month < 10 ? '0' + month : month
        let day = date1.getDate()
        day = day < 10 ? '0' + day : day
        let hours = date1.getHours()
        hours = hours < 10 ? '0' + hours : hours
        let min = date1.getMinutes()
        min = min < 10 ? '0' + min : min
        let sc = date1.getSeconds()
        sc = sc < 10 ? '0' + sc : sc
        let time = `${year}-${month}-${day} ${hours}-${min}-${sc}`
        arrays[index] = {
          date: date1,
          time: time, // 发布时间
          type: item.type, // 客户端类型
          version: item.version,  // 版本号
          files: item.files,  // OSS文件名字
          addfun: item.addfun.split('\n'), // 添加的功能
          debug: item.debug.split('\n') // 修复的功能
        }
      })
      arrays.sort(function (x,y) {
        return y.date - x.date
      })
    } catch (e) {
      this.ctx.fail( 1000, e.message )
    }
    if (this.ctx.method === 'GET') {
      this.assign({
        type: this.get().type,
        list: arrays
      })
      return this.display()
    } else {
      this.json( { status: 1, list: arrays})
    }
  }
}